/**
  * AntRanking Copyright (c) 2008-2012 INRIA, University of Lille 1
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  * 
  * Contact: 
  * 	benjamin.bertein@gmail.com,
  * 	nacim.hamdad@gmail.com
  * 	nicolas.haderer@inria.fr
  *
  * Author: Nacim Hamdad,Benjamin Bertein, Haderer Nicolas
  */
package com.google.antranking.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class AntDroidDatabase {

	final static public Integer VERSION = 1;

	final static public String DATABASE_NAME = "AntDroidDB";

	final static public String TABLE_REMOTE_APPS = "RemoteAppsTable";
	final static public String TABLE_REMOTE_APPS_FIELD_ID = "field_id";
	final static public String TABLE_REMOTE_APPS_FIELD_NAME = "field_name";
	final static public String TABLE_REMOTE_APPS_FIELD_OBJECT = "field_object";

	private SQLiteDatabase db;

	private SQLiteOpenHelper helper;


	public AntDroidDatabase(Context ctx){
		this.helper = new SensingAppsDatabaseHelper(ctx);
	}


	public void open(){
		this.db = this.helper.getWritableDatabase();
	}

	public void close(){
		helper.close();
		this.db.close();
	}

	public void clearRemoteAppsTable(){
		this.db.delete(TABLE_REMOTE_APPS, "", null);
	}

	public void addRemoteAppsEntry(String name, String jsonObject){
		
		final StringBuilder query = new StringBuilder();
		query.append("INSERT INTO "+TABLE_REMOTE_APPS);
		query.append(" ("+TABLE_REMOTE_APPS_FIELD_NAME+","+TABLE_REMOTE_APPS_FIELD_OBJECT+") ");
		query.append(" VALUES ( ");
		query.append(" '"+name+"', ");
		query.append(" \""+jsonObject.replaceAll("\"","'")+"\" ");
		query.append(" );");
		
		this.db.execSQL(query.toString());
	}

	public String getRemoteAppsEntry(String name){

		String result = null;
		
		final StringBuilder query = new StringBuilder();
		query.append("SELECT "+TABLE_REMOTE_APPS_FIELD_OBJECT+" FROM "+TABLE_REMOTE_APPS+" WHERE "+TABLE_REMOTE_APPS_FIELD_NAME+"='"+name+"'");

		final Cursor cursor = db.rawQuery(query.toString(), null);
		while(cursor.moveToNext()){
			result  = cursor.getString(cursor.getColumnIndex(TABLE_REMOTE_APPS_FIELD_OBJECT));
		}
		cursor.close();
		return result;
	}

	public String[] getAllRemoteEntry(){

		

		final StringBuilder query = new StringBuilder();
		query.append("SELECT "+TABLE_REMOTE_APPS_FIELD_OBJECT+" FROM "+TABLE_REMOTE_APPS);

		final Cursor cursor = db.rawQuery(query.toString(), null);
		
		final String[] results = new String[cursor.getCount()];
		int i = 0; 
		while(cursor.moveToNext()){
			results[i] = cursor.getString(cursor.getColumnIndex(TABLE_REMOTE_APPS_FIELD_OBJECT));
			i++;
		}
		cursor.close();
		return results;
	}

	public class SensingAppsDatabaseHelper extends SQLiteOpenHelper{

		public SensingAppsDatabaseHelper(Context context) {
			super(context, DATABASE_NAME,null,VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {

			final StringBuffer query = new StringBuffer();
			query.append("CREATE TABLE ");
			query.append(TABLE_REMOTE_APPS);
			query.append(" ( ");
			query.append(" " + TABLE_REMOTE_APPS_FIELD_ID+ " integer primary key autoincrement, ");
			query.append(" " + TABLE_REMOTE_APPS_FIELD_NAME + " text not null, ");
			query.append(" " + TABLE_REMOTE_APPS_FIELD_OBJECT + " text not null ");
			query.append(" ); ");

			// exec query
			db.execSQL(query.toString());
		}

		@Override
		public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {}
	}
}
